package com.sgb.kjwl.viewmodel.manage;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.TwoLevelChildForPopWindowEntity;
import com.sgb.kjwl.model.entity.TwoLevelForPopWindowEntity;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntityForManageList;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ManageListViewModel extends AndroidViewModel {

    public MutableLiveData<List<TheEnterpriseOfEntityForManageList>> mEnterpriseList = new MutableLiveData<>();
    public MutableLiveData<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>> mIndustryType = new MutableLiveData<>();
    public MutableLiveData<Boolean> mSuccessFollow = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mCityList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> mHonorType = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> mSpecialType = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity<List<PickSelectEntity>>>> mManageType = new MutableLiveData<>();
    public MutableLiveData<Boolean> mLoading = new MutableLiveData<>();
    public MutableLiveData<Boolean> mSuccess = new MutableLiveData<>();
    public MutableLiveData<Boolean> mError = new MutableLiveData<>();

    public ManageListViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void getManageList(Map<String,Object> map){
        mLoading.postValue(true);
        APIManager.getInstance().getManageAPI().getComList2(map).enqueue(new Callback<BaseEntity<ListResponse<List<TheEnterpriseOfEntityForManageList>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TheEnterpriseOfEntityForManageList>>>> call, Response<BaseEntity<ListResponse<List<TheEnterpriseOfEntityForManageList>>>> response) {
                mSuccess.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    mEnterpriseList.postValue(response.body().getData().getList());
                } else {
                    MToast.showLongToast(getApplication().getApplicationContext(), "数据异常");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TheEnterpriseOfEntityForManageList>>>> call, Throwable t) {
                mError.postValue(true);
            }
        });
    }

    public void follow(String shopId) {
        Map<String,Object> map = new HashMap<>();
        map.put("businessNo",shopId);
        map.put("type",0);
        mLoading.postValue(true);
        APIManager.getInstance().getManageAPI().follow(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                mSuccess.postValue(true);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mSuccessFollow.postValue(true);
                } else {
                    MToast.showToast(getApplication().getApplicationContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                mError.postValue(true);
            }
        });
    }

    public void getTradeTypes() {
        mLoading.postValue(true);
        APIManager.getInstance().getManageAPI().getCompanyTradeType().enqueue(new Callback<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>> call, Response<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>> response) {
                mSuccess.postValue(true);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mIndustryType.postValue(response.body().getData());
                } else {
                    MToast.showToast(getApplication().getApplicationContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>> call, Throwable t) {
                mError.postValue(true);
            }
        });
    }

    public void getCityData() {
        mLoading.postValue(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                mSuccess.postValue(true);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mCityList.postValue(response.body().getData());
                } else {
                    MToast.showToast(getApplication().getApplicationContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                mError.postValue(true);
            }
        });
    }

    public void getHonorType() {
        mLoading.postValue(true);
        APIManager.getInstance().getManageAPI().getHonorType().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                mSuccess.postValue(true);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> list = response.body().getData();
                    PickSelectEntity pickSelectEntity = new PickSelectEntity();
                    pickSelectEntity.setProjectscale("不限");
                    pickSelectEntity.setNo("");
                    list.add(0, pickSelectEntity);
                    mHonorType.postValue(list);
                } else {
                    MToast.showToast(getApplication().getApplicationContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                mError.postValue(true);
            }
        });
    }

    public void findMyListByCompanyInfoNo() {
        mLoading.postValue(true);
        APIManager.getInstance().getManageAPI().getQualifyTypeTreeList().enqueue(new Callback<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> call, Response<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> response) {
                mSuccess.postValue(true);
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(getApplication().getApplicationContext(), "暂无数据");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> list = new ArrayList<>();
                    for (int i = 0; i < response.body().getData().size(); i++) {
                        list.add(new PickSelectEntity(response.body().getData().get(i).getNo(),response.body().getData().get(i).getName()));
                    }
                    PickSelectEntity entity = new PickSelectEntity();
                    entity.setNo("");
                    entity.setProjectscale("不限");
                    list.add(0, entity);
                    mSpecialType.postValue(list);
                } else {
                    MToast.showToast(getApplication().getApplicationContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> call, Throwable t) {
                mError.postValue(true);
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void getTypeList() {
        mLoading.postValue(true);
        APIManager.getInstance().getManageAPI().getTypeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Response<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> response) {
                mSuccess.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity<List<PickSelectEntity>>> list = new ArrayList<>();
                    list.add(0,new PickSelectEntity("","全部"));
                    list.addAll(response.body().getData());
                    mManageType.postValue(list);
                } else {
                    MToast.showLongToast(getApplication().getApplicationContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Throwable t) {
                mError.postValue(true);
            }
        });
    }
}
